/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       MdmsDatabaseController.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/11/13      feiwe                         add
 */


package com.fengwei.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengwei.form.MapForObject.MapForObject;
import com.fengwei.requestdata.database.MdmsDatabaseAddData;
import com.fengwei.requestdata.database.MdmsDatabaseDelData;
import com.fengwei.requestdata.database.MdmsDatabaseEditData;
import com.fengwei.requestdata.database.MdmsDatabasePagingListData;
import com.fengwei.requestdata.database.MdmsDatabaseQueryAllTablesData;
import com.fengwei.requestdata.database.MdmsDatabaseQueryMyDatabasesData;
import com.fengwei.requestdata.database.MdmsDatabaseTestConnectionData;
import com.fengwei.service.ConsoleCommonService;
import com.fengwei.service.MdmsDatabaseService;
import com.fengwei.util.PltResult;
import com.fengwei.util.ResponseUtil;
import com.fengwei.verify.Verify;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
public class MdmsDatabaseController {
    private final static Logger logger = LoggerFactory.getLogger(MdmsDatabaseController.class);

    @Autowired
    private MdmsDatabaseService mdmsDatabaseService;

    @Autowired
    private ConsoleCommonService consoleCommonService;

    @GetMapping(value = "/mdmsDatabase/loadPage")
    public String loadPage(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "mdmsDatabase";
    }

    /**
     * 数据库列表
     */
    @PostMapping(path = "/mdmsDatabase/pagingList")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> pagingList(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsDatabasePagingListData mdmsDatabasePagingListData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<MdmsDatabasePagingListData>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatabasePagingListData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatabaseService.pagingList(mdmsDatabasePagingListData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 数据库用户
     */
    @PostMapping(path = "/mdmsDatabase/add")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> add(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsDatabaseAddData mdmsDatabaseAddData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<MdmsDatabaseAddData>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatabaseAddData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatabaseService.add(mdmsDatabaseAddData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 数据库用户
     */
    @PostMapping(path = "/mdmsDatabase/edit")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> edit(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsDatabaseEditData mdmsDatabaseEditData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<MdmsDatabaseEditData>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatabaseEditData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatabaseService.edit(mdmsDatabaseEditData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 数据库用户
     */
    @PostMapping(path = "/mdmsDatabase/del")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> del(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsDatabaseDelData mdmsDatabaseDelData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<MdmsDatabaseDelData>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatabaseDelData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatabaseService.del(mdmsDatabaseDelData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 数据库连接测试
     */
    @PostMapping(path = "/mdmsDatabase/testConnetion")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> testConnetion(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsDatabaseTestConnectionData mdmsDatabaseTestConnectionData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatabaseTestConnectionData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatabaseService.testConnetion(mdmsDatabaseTestConnectionData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 获取用户自己的数据库连接
     */
    @PostMapping(path = "/mdmsDatabase/queryMyDatabases")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN','DBA','USER')")
    public Map<String, Object> queryMyDatabases(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsDatabaseQueryMyDatabasesData mdmsDatabaseQueryMyDatabasesData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<MdmsDatabaseQueryMyDatabasesData>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatabaseQueryMyDatabasesData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatabaseService.queryMyDatabases(mdmsDatabaseQueryMyDatabasesData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 获取用户自己的数据库连接
     */
    @PostMapping(path = "/mdmsDatabase/queryAllTables")
    @ResponseBody
    @PreAuthorize("hasAnyRole('DBA','USER')")
    public Map<String, Object> queryAllTables(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsDatabaseQueryAllTablesData mdmsDatabaseQueryAllTablesData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<MdmsDatabaseQueryAllTablesData>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsDatabaseQueryAllTablesData);
        // 正式处理业务
        Map<String, Object> data = mdmsDatabaseService.queryAllTables(mdmsDatabaseQueryAllTablesData, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }
}
